package xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor;

import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.command.edit.EditCommand;
import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.memento.Memento;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author xing
 * @date 2023/2/10 15:09:56
 */
public class History {
    Deque<Pair> undoStack = new ArrayDeque<>();
    Deque<EditCommand> redoStack = new ArrayDeque<>();
    private Editor editor;

    public History(Editor editor) {
        this.editor = editor;
    }

    public void undoPush(EditCommand c, Memento m) {
        undoStack.push(new Pair(c, m));
    }

    public void redoPush(EditCommand c) {
        redoStack.push(c);
    }

    public Pair undoPop() {
        Pair pair = undoPeek();
        return pair == null ? null : undoStack.pop();
    }

    public EditCommand redoPop() {
        EditCommand c = redoStack.peek();
        return c == null ? null : redoStack.pop();
    }

    public Pair undoPeek() {
        return undoStack.peek();
    }


    /**
     * 这里为了测试，在 Memento 暴露了其中 content 信息。
     * <p>
     * 实际如果要暴露，可以只在这里暴露 命令 的 History
     */

    public void showAllUndoHis() {
        System.out.println("----");
        for (Pair pair : undoStack) {
            System.out.println(pair.getC().getName() + " : " + pair.getM().getContent());
        }
        System.out.println("----");
    }
}
